package cn.sjx.graduate.service.impl;

import cn.sjx.graduate.domain.Department;
import cn.sjx.graduate.domain.Employee;
import cn.sjx.graduate.domain.Staff;
import cn.sjx.graduate.domain.StaffType;
import cn.sjx.graduate.mapper.StaffMapper;
import cn.sjx.graduate.qo.StaffTypeQuery;
import cn.sjx.graduate.service.IDepartmentService;
import cn.sjx.graduate.service.IEmployeeService;
import cn.sjx.graduate.service.IStaffService;
import cn.sjx.graduate.service.IStaffTypeService;
import cn.sjx.graduate.vo.StaffVO;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: bo-feifei
 * @Date 2022/1/17 15:41
 * @Version 1.0
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IStaffTypeService staffTypeService;
    @Autowired
    private IDepartmentService departmentService;

    @Override
    public IPage<StaffVO> queryPage(StaffTypeQuery qo, Employee employee) {
        IPage<StaffVO> page = new Page<>();

        Integer current = qo.getCurrentPage();
        Integer size = qo.getPageSize();
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 10;
        }
        Boolean admin = employee.getAdmin();
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        if (!admin) {
            wrapper.eq("employee_id", employee.getId());
        }
        if (qo.getKeyword() != null && !qo.getKeyword().equals("")) {
            wrapper.eq("name", qo.getKeyword());
        }
        wrapper.eq("deleted",0);
        List<Staff> records = list(wrapper);
        Integer total = records.size();
        if (!records.isEmpty()) {
            try {
                if (current * size - 1 <= records.size()) {
                    records = records.subList((current - 1) * size, current * size);
                } else {
                    records = records.subList((current - 1) * size, records.size());
                }
            } catch (Exception e) {
                records = Lists.newArrayList();
            }
        }
        Integer pages = total % size == 0 ? total / size : (total / size + 1);

        List<StaffVO> voList = new ArrayList<>();
        StaffVO staffVO = null;
        for (Staff record : records) {
            staffVO = new StaffVO();
            BeanUtils.copyProperties(record, staffVO);
            // 获取这个教职员工的教师类型
            Integer type = record.getType();
            StaffType staffType = staffTypeService.getById(type);
            staffVO.setStaffTypeName(staffType.getStaffType());
            // 获取这个教职员工对应的员工id
            Long employeeId = record.getEmployeeId();
            Employee employee1 = employeeService.getById(employeeId);
            Integer dept = employee1.getDept();
            Department department = departmentService.getById(dept);
            staffVO.setDept(dept);
            staffVO.setDeptName(department.getName());
            voList.add(staffVO);
        }
        page.setRecords(voList);
        page.setCurrent(current);
        page.setSize(size);
        page.setTotal(total);
        page.setPages(pages);

        return page;
    }


}
